package com.cly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.base.contants.CacheKeysConstants;
import com.cly.entity.Subject;
import com.cly.mapper.SubjectMapper;
import com.cly.result.JsonRequest;
import com.cly.result.exception.ServiceException;
import com.cly.result.exenum.RequestException;
import com.cly.service.ISubjectService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 科目表业务层实现
 *
 * @author 春风能解释
 * <p>
 * 2024-06-06
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements ISubjectService {


    /**
     * 条件查询科目表信息
     *
     * @param subject 科目表条件
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.CourseKeys.SUBJECT_ARRAY, key = "#root.args")
    public JsonRequest<List<Subject>> lookAllSubject(Subject subject) {
        return JsonRequest.success(baseMapper.selectSubjectList(subject));
    }

    /**
     * 根据条件查询科目表信息
     *
     * @param subject 条件
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.CourseKeys.SUBJECT_ARRAY, key = "#root.args")
    public JsonRequest<List<Subject>> lookSubject(Integer current, Integer size, Subject subject) {
        IPage<Subject> subjectPage = baseMapper.selectPage(new Page<>(current, size), new LambdaQueryWrapper<Subject>()
                .eq(subject.getId() != null, Subject::getId, subject.getId())
                .like(StringUtils.hasText(subject.getName()), Subject::getName, subject.getName()));
        // 传入分页
        return JsonRequest.success(subjectPage.getRecords(), subjectPage.getTotal());
    }

    /**
     * 新增一条科目表信息
     *
     * @param subject 科目表信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.CourseKeys.SUBJECT_ARRAY, allEntries = true)
    public JsonRequest<Integer> addOneSubject(Subject subject) {
        // 判断科目表是否存在
        Integer size = baseMapper.countSubjectByCondition(subject);
        if (size > 0) {
            throw new ServiceException(RequestException.DATA_EXISTS_ERROR);
        }
        // 插入科目表信息
        int column = baseMapper.insert(subject);
        if (column < 1) {
            throw new ServiceException(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 修改一条科目表信息
     *
     * @param subject 科目表信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.CourseKeys.SUBJECT_ARRAY, allEntries = true)
    public JsonRequest<Integer> updateOneSubject(Subject subject) {
        Subject subjectObject = baseMapper.selectById(subject.getId());
        if (Objects.isNull(subjectObject)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        // 判断待修改的科目表信息是否已经存在
        Integer size = baseMapper.countSubjectByCondition(subject);
        if (size > 0) {
            throw new ServiceException(RequestException.DATA_EXISTS_ERROR);
        }
        int column = baseMapper.updateById(subject);
        if (column < 1) {
            throw new ServiceException(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 批量删除科目表信息
     *
     * @param ids 科目表id列表
     * @return JSON
     */
    @Override
    @CacheEvict(value = CacheKeysConstants.CourseKeys.SUBJECT_ARRAY, allEntries = true)
    public JsonRequest<Integer> deleteSubjects(List<Long> ids) {
        // 查询科目表信息
        List<Subject> subjectList = baseMapper.selectBatchIds(ids);
        // 判断待删除的科目表信息是否存在
        if (subjectList.size() != ids.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = baseMapper.deleteByIds(ids);
        if (column < 1) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 根据ID查询科目信息并转换为Map
     *
     * @param ids id
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.CourseKeys.SUBJECT_ARRAY, key = "#root.args")
    public JsonRequest<Map<Long, Subject>> lookSubjectMap(List<Long> ids) {
        Map<Long, Subject> map = new HashMap<>();
        listByIds(ids).forEach(v -> map.put(v.getId(), v));
        return JsonRequest.success(map);
    }

}